/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * A simple agent performing rdg of <code>String</code>.
 * <br>
 * Allows to select scope.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class Rdg implements Runnable 
{
 //	identification number	
 private int id = new Random().nextInt(1000);		
	
 // private data members
 private Location cur = Location.HERE;
 private Location dest = Location.UNSPECIFIED;
 
 // String values for "here" and "unspecified"
 private final String HereS = "here";
 private final String UnspS = "?"; 
 
 /**
  * Constructor method (default). Current location is "here" and destination location is "unspecified".
  */
 public Rdg()
 {
  // empty	
 }
 
 /**
  * Constructor method. Destination location is "unspecified".
  *
  * @param a [current location = unspecified, here]
  */
 public Rdg(String a)
 {
  if (!a.equals(HereS) && !a.equals(UnspS)) throw new IllegalArgumentException("if you pass only one parameter, it must be " + UnspS + "/" + HereS);	
  if (a.equals(UnspS)) cur = Location.UNSPECIFIED;	
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of current location] [current location = unspecified, here]
  * @param b [port of current location] [destination location = unspecified, here]
  */
 public Rdg(String a, String b)
 {
  if ((!a.equals(HereS) && !a.equals(UnspS)) || (!b.equals(HereS) && !b.equals(UnspS)))
  	 cur = Location.build(a, new Integer(b).intValue());
  else
  {	
   if (a.equals(UnspS)) cur = Location.UNSPECIFIED;
   if (b.equals(HereS)) dest = Location.HERE;
  }
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of current location] [current location = unspecified, here]
  * @param b [port of current location] [name/IP of destination location]
  * @param c [destination location = unspecified, here] [port of destination location]
  */
 public Rdg(String a, String b, String c)
 {
  if (a.equals(HereS)) dest = Location.build(b, new Integer(c).intValue()); 
  else
  if (a.equals(UnspS)) { cur = Location.UNSPECIFIED; dest = Location.build(b, new Integer(c).intValue()); }
  else
  if (c.equals(HereS)) { cur = Location.build(a, new Integer(b).intValue()); dest = Location.HERE; } 
  else
  if (c.equals(UnspS)) { cur = Location.build(a, new Integer(b).intValue()); }
  else
  throw new IllegalArgumentException("if you pass only three parameters, cur or dest must be " + UnspS + "/" + HereS);
 }
 
 /**
  * Constructor method.
  * 
  * @param a [name/IP of current location]
  * @param b [port of current location]
  * @param c [name/IP of destination location]
  * @param d [port of destination location]
  */
 public Rdg(String a, String b, String c, String d)
 {
  cur = Location.build(a, new Integer(b).intValue());
  dest = Location.build(c, new Integer(d).intValue());
 } 
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("Rdg " + id + ": started");     
  LimeTupleSpace lts = LimeTupleSpace.get("testing"); 
  ITuple pattern = new Tuple().add(new Field().setType(String.class));
  LimeTemplate template = new LimeTemplate(cur, dest, pattern);
  LimeTuple[] lt = lts.rdg(template);
  if (lt != null)
  {
   System.out.println("Rdg " + id + ": has found the following");
   for (int i = 0; i < lt.length; i++) System.out.println("Rdg " + id + ": [" + lt[i].getID() + "] " + lt[i].getTuple());
  }
  else System.out.println("Rdg " + id + ": has found null");
  System.out.println("Rdg " + id + ": dies");
 }

} 
